# @Remark:登录页面
# login_page.py模块下就是存放登录页面上的各个功能点的。（登录功能、获取登录失败的提示信息）

import time
from Common.basepage import BasePage
from PageLocators.login.login_page_locator import LoginPageLocator as loc


class LoginPage(BasePage):
    # 登录功能
    def login(self, username, pwd, user_loc=loc.user_loc, login_button_loc=loc.login_button_loc):
        time.sleep(3)
        self.input_text(user_loc, "登录页面_输入用户名", username)
        self.input_text(loc.pwd_loc, "登录页面_输入密码", pwd)
        # time.sleep(5)
        self.click_element(login_button_loc, "登录页面_点击登录按钮")

    # 退出登录
    def logout(self):
        # self.click_element(loc.drop_down_loc, "退出下拉框")
        self.mouse_suspension(loc.drop_down_loc, "退出下拉框")
        time.sleep(2)
        self.click_element(loc.logout_loc, "退出")

    # 跳转业务线-学术研究中心
    def go_bus(self):
        self.click_element(loc.business_loc, "跳转学术研究中心")
        time.sleep(3)

    # 跳转业务线-学术研究中心
    def go_ar(self):
        self.click_element(loc.business_loc, "跳转学术研究中心")
        time.sleep(3)

    def go_crm(self):
        self.click_element(loc.crm_loc, "跳转CRM平台")

    def go_ts(self):
        self.click_element(loc.ts_loc, "跳转班主任工作平台")
        time.sleep(3)

    def go_crm_work(self):
        self.click_element(loc.crm_work_loc, "跳转CRM工作平台")
        time.sleep(3)

    # 切换窗口
    def switch_window(self, title="统一登录平台"):
        try:
            handles = self.driver.window_handles
            for handle in handles:  # 历遍所有标签的句柄
                self.driver.switch_to.window(handle)  # 通过句柄切换到该浏览器的标签
                if title in self.driver.title:  # driver.title当前标签名
                    print(self.driver.title, self.driver.current_url, self.driver.current_window_handle, handle)
                    break  # 退出循环
            time.sleep(10)
            return True
        except:
            return False

    # 学术研究中心-知识点页面
    def knowledge_page(self):
        try:
            handles = self.driver.window_handles
            for handle in handles:  # 历遍所有标签的句柄
                self.driver.switch_to.window(handle)  # 通过句柄切换到该浏览器的标签
                if '学术研发平台' in self.driver.title:  # driver.title当前标签名
                    print(self.driver.title, self.driver.current_url, self.driver.current_window_handle, handle)
                    break  # 退出循环
            # self.driver.switch_to.window(self.driver.window_handles[1])
            time.sleep(3)
            self.input_text(loc.knowledge_points, "知识点名称输入", "hahaha知识点")
            self.input_text(loc.class_times, "输入课次", "hahaha课次")
            time.sleep(10)
            return True
        except:
            return False
        # self.driver.close()

    # 获取登录失败提示信息
    def get_errorMsg(self):
        return self.get_element_text(loc.login_error_loc,"登录失败错误提示！")

    def assert_welcome(self):
        return self.get_element(loc.home_welcome, img_doc='登录完成_获取首页元素')

    def assert_widow_url(self):
        print(self.driver.current_url)
        return self.driver.current_url